package site.jlopen.secret.service.impl;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import site.jlopen.secret.client.SecretClientHttp;
import site.jlopen.secret.client.SecretReqUrl;
import site.jlopen.secret.common.SecretClientResponse;
import site.jlopen.secret.common.SecretReqBaseForm;
import site.jlopen.secret.config.SecretClientProperties;
import site.jlopen.secret.service.intf.SecretRouteManage;

public class SecretRouteImpl implements SecretRouteManage {
	private final Logger logger = LoggerFactory.getLogger(getClass());

	private final SecretClientProperties clientProperties;
	
	public SecretRouteImpl(SecretClientProperties clientProperties) {
		this.clientProperties = clientProperties;
	}

	@Override
	public SecretClientResponse<String> bindNodeRoute(String srcNodeCode, String destNodeCode, int bindType) {
		logger.info("绑定{}节点跟{}节点路由关系", srcNodeCode, destNodeCode);
		SecretClientHttp<String> clientRequest = new SecretClientHttp<String>(clientProperties);
		Map<String, Object> form = new SecretReqBaseForm<Object>().add("srcNodeCode", srcNodeCode).add("destNodeCode", destNodeCode)
				.add("bindType", bindType).appId(clientProperties.getAppId());
		SecretClientResponse<String>  res = clientRequest.execute(SecretReqUrl.ROUTE_BIND_URL, form);
		return res;
	}

	@Override
	public SecretClientResponse<String> refreshNodeRoute(String srcNodeCode, String destNodeCode) {
		logger.info("刷新{}节点跟{}节点路由关系状态", srcNodeCode, destNodeCode);
		SecretClientHttp<String> clientRequest = new SecretClientHttp<String>(clientProperties);
		Map<String, Object> form = new SecretReqBaseForm<Object>().add("srcNodeCode", srcNodeCode).add("destNodeCode", destNodeCode)
				.appId(clientProperties.getAppId());
		SecretClientResponse<String>  res = clientRequest.execute(SecretReqUrl.ROUTE_REFRESH_URL, form);
		return res;
	}

	@Override
	public SecretClientResponse<String> existNodeRoute(String srcNodeCode, String destNodeCode) {
		logger.info("判断{}节点跟{}节点路由关系情况", srcNodeCode, destNodeCode);
		SecretClientHttp<String> clientRequest = new SecretClientHttp<String>(clientProperties);
		Map<String, Object> form = new SecretReqBaseForm<Object>().add("srcNodeCode", srcNodeCode).add("destNodeCode", destNodeCode)
				.appId(clientProperties.getAppId());
		SecretClientResponse<String>  res = clientRequest.execute(SecretReqUrl.ROUTE_EXIST_URL, form);
		return res;
	}

	@Override
	public SecretClientResponse<String> unbindNodeRoute(String srcNodeCode, String destNodeCode) {
		logger.info("解绑{}节点跟{}节点路由关系", srcNodeCode, destNodeCode);
		SecretClientHttp<String> clientRequest = new SecretClientHttp<String>(clientProperties);
		Map<String, Object> form = new SecretReqBaseForm<Object>().add("srcNodeCode", srcNodeCode).add("destNodeCode", destNodeCode)
				.appId(clientProperties.getAppId());
		SecretClientResponse<String>  res = clientRequest.execute(SecretReqUrl.ROUTE_UNBIND_URL, form);
		return res;
	}

}
